package cn.itcast.mapper;

import cn.itcast.domain.Dept;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface DeptMapper {
    //查询所有
    @Select("select * from ss_dept")
    @Results({
            @Result(id = true,column = "dept_id",property = "id"),
            @Result(column = "dept_name",property = "deptName"),
            @Result(column = "parent_id",property = "parentId"),
            @Result(column = "state",property = "state"),
            @Result(property = "parent",javaType = Dept.class,column = "parent_id",
            one = @One(select = "cn.itcast.mapper.DeptMapper.findById"))
    })
    List<Dept> findAll();

    @Select("select * from ss_dept where dept_id = #{pid}")
    @Results({
            @Result(id = true,column = "dept_id",property = "id"),
            @Result(column = "dept_name",property = "deptName"),
            @Result(column = "parent_id",property = "parentId"),
            @Result(property = "parent",javaType = Dept.class,column = "parent_id",
                    one = @One(select = "cn.itcast.mapper.DeptMapper.findById"))
    })
    Dept findById(String pid);


    //添加用户
    @Insert(" insert into ss_dept (dept_id, dept_name, parent_id,state)\n" +
            "values (#{id,jdbcType=VARCHAR}, #{deptName,jdbcType=VARCHAR}," +
            " #{parentId,jdbcType=VARCHAR},#{state,jdbcType=DECIMAL})")
    void add(Dept dept);
    //修改用户
    @Update("update ss_dept\n" +
            "        set dept_name = #{deptName},\n" +
            "        parent_id = #{parentId},\n" +
            "        state = #{state}\n" +
            "        where dept_id = #{id}")
    void edit(Dept dept);

    //批量删除
    void deleteByIds(String[] id);


    //
    @Select("select * from ss_dept where dept_id =#{uid}")
    @Results({
            @Result(id = true,column = "dept_id",property = "id"),
            @Result(column = "dept_name",property = "deptName"),
    })
    Dept findByUid(String uid);


}
